package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ExtHelper;
import com.ruoyi.common.utils.SpellUtils;
import com.ruoyi.common.utils.uuid.IdGeneratorSnowflake;
import com.ruoyi.system.domain.TrainingBase;
import com.ruoyi.system.mapper.TrainingBaseMapper;
import com.ruoyi.system.service.ITrainingBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 临床培训基地Service业务层处理
 *
 * @author dp
 * @date 2023-11-27
 */
@Service
public class TrainingBaseServiceImpl implements ITrainingBaseService {
    @Autowired
    TrainingBaseMapper trainingBaseMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询临床培训基地
     *
     * @param baseId 临床培训基地主键
     * @return 临床培训基地
     */
    @Override
    public TrainingBase selectTrainingBaseByBaseId(Long baseId) {
        return trainingBaseMapper.selectTrainingBaseByBaseId(baseId);
    }

    /**
     * 查询临床培训基地列表
     *
     * @param trainingBase 临床培训基地
     * @return 临床培训基地
     */
    @Override
    public List<TrainingBase> selectTrainingBaseList(TrainingBase trainingBase) {
        return trainingBaseMapper.selectTrainingBaseList(trainingBase);
    }

    /**
     * 新增临床培训基地
     *
     * @param trainingBase 临床培训基地
     * @return 结果
     */
    @Override
    public int insertTrainingBase(TrainingBase trainingBase) {
        trainingBase.setCreateTime(DateUtils.getNowDate());
        return trainingBaseMapper.insertTrainingBase(trainingBase);
    }

    /**
     * 修改临床培训基地
     *
     * @param trainingBase 临床培训基地
     * @return 结果
     */
    @Override
    public int updateTrainingBase(TrainingBase trainingBase) {
        return trainingBaseMapper.updateTrainingBase(trainingBase);
    }

    /**
     * 批量删除临床培训基地
     *
     * @param baseIds 需要删除的临床培训基地主键
     * @return 结果
     */
    @Override
    public int deleteTrainingBaseByBaseIds(Long[] baseIds) {
        return trainingBaseMapper.deleteTrainingBaseByBaseIds(baseIds);
    }

    /**
     * 删除临床培训基地信息
     *
     * @param baseId 临床培训基地主键
     * @return 结果
     */
    @Override
    public int deleteTrainingBaseByBaseId(Long baseId) {
        return trainingBaseMapper.deleteTrainingBaseByBaseId(baseId);
    }

    /**
     * 实体数据补全
     *
     * @param trainingBase 实体
     * @param userId       用户id
     * @param crateType    创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(TrainingBase trainingBase, Long userId, Integer crateType) {
        //主键
        if (trainingBase.getBaseId() == null||"".equals(trainingBase.getBaseId())) {
            trainingBase.setBaseId(ExtHelper.toString(idGeneratorSnowflake.nextId()));
        }
        //编码
        if (trainingBase.getBaseCode() == null||"".equals(trainingBase.getBaseCode())) {
            trainingBase.setBaseCode(SpellUtils.convertSpell(trainingBase.getBaseName(), false));
        }
        //描述
        if (trainingBase.getBaseDesc() == null) {
            trainingBase.setBaseDesc("");
        }
        //是否有效
        if (trainingBase.getIsValid() == null) {
            trainingBase.setIsValid(1L);
        }
        //创建人、创建时间
        if (crateType == 1) {
            if (trainingBase.getCreateBy() == null) {
                trainingBase.setCreateBy(userId.toString());
            }
            if (trainingBase.getCreateTime() == null) {
                trainingBase.setCreateTime(DateUtils.getNowDate());
            }
        }
        //修改人
        if (trainingBase.getModifyBy() == null) {
            trainingBase.setModifyBy(userId);
        }
        //修改时间
        if (trainingBase.getModifyTime() == null) {
            trainingBase.setModifyTime(DateUtils.getNowDate());
        }
    }
}
